package com.supostacks.xuetangerp.controller;

import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSONObject;
import com.supostacks.rpc.server.IDataDeployService;
import com.supostacks.rpc.server.IDataFetchService;
import com.supostacks.rpc.server.dto.BaseDto;
import com.supostacks.rpc.server.req.DeployReq;
import com.supostacks.rpc.server.req.FetchReq;
import com.supostacks.rpc.server.res.XResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
public abstract class BaseController {

    @DubboReference
    public IDataFetchService dataFetchService;

    @DubboReference
    public IDataDeployService dataDeployService;


    @RequestMapping("/insert")
    public SaResult insert(@RequestBody JSONObject json) {
        BaseDto baseDto = buildData(json);

        DeployReq req = DeployReq.builder().id(baseDto.getId())
                .data(baseDto).type("insert").build();

        XResult res = dataDeployService.deploy(req);
        return SaResult.code(res.getCode()).setMsg(res.getMsg()).setData(res.getResult());
    }

    @RequestMapping("/update")
    public SaResult update(@RequestBody JSONObject json) {
        BaseDto baseDto = buildData(json);

        DeployReq req = DeployReq.builder().id(baseDto.getId())
                .data(baseDto).type("update").build();

        XResult res = dataDeployService.deploy(req);
        return SaResult.code(res.getCode()).setMsg(res.getMsg()).setData(res.getResult());
    }

    @RequestMapping("/delete")
    public SaResult delete(String id) {
        BaseDto baseDto = takeBaseDto();

        DeployReq req = DeployReq.builder().id(id)
                .data(baseDto).type("delete").build();

        XResult res = dataDeployService.deploy(req);
        return SaResult.code(res.getCode()).setMsg(res.getMsg()).setData(res.getResult());
    }

    @RequestMapping("/select/list")
    public SaResult selectList() {
        BaseDto baseDto = takeBaseDto();
        FetchReq req = FetchReq.builder()
                .data(baseDto).type("selectList").build();
        XResult res = dataFetchService.fetch(req);
        return SaResult.code(res.getCode()).setMsg(res.getMsg()).setData(res.getResult());
    }

    @RequestMapping("/select/by/id")
    public SaResult selectById(@RequestParam("id") String id) {
        BaseDto baseDto = takeBaseDto();

        JSONObject json = new JSONObject();
        json.put("id", id);

        FetchReq req = FetchReq.builder()
                .option(json)
                .data(baseDto).type("selectById").build();

        XResult res = dataFetchService.fetch(req);
        return SaResult.code(res.getCode()).setMsg(res.getMsg()).setData(res.getResult());
    }

    @RequestMapping("/select/by/openid")
    public SaResult selectByOpenid(String openid) {
        BaseDto baseDto = takeBaseDto();
        JSONObject param = new JSONObject();
        param.put("openid", openid);
        FetchReq req = FetchReq.builder().option(param).type("selectByOpenid")
                .data(baseDto).build();
        XResult res = dataFetchService.fetch(req);
        return SaResult.code(res.getCode()).setMsg(res.getMsg()).setData(res.getResult());
    }

    @PostMapping("/select/by/page")
    public SaResult selectByPage(@RequestBody JSONObject json){
        BaseDto baseDto = takeBaseDto();
        FetchReq req = FetchReq.builder().option(json).type("selectByPage")
                .data(baseDto).build();
        XResult res = dataFetchService.fetch(req);
        return SaResult.code(res.getCode()).setMsg(res.getMsg()).setData(res.getResult());
    }


    public abstract BaseDto buildData(JSONObject json);
    public abstract BaseDto takeBaseDto();

}
